/**
 * 系统存储对象，用于存储系统级别的数据
 */
import { sysDict } from '@/api/platformApi'
import { sysMenu } from '@/api/business/businessApi'
import buildMenus from '@/utils/menuTree'

/**
 * 系统数据存储
 */

 const sys = {
    state: {
      // 字典
      dictionarys: [],
      // 系统菜单栏
      navMenus: []
    },
    mutations: {
      /**
       * 追加一个字典项
       * @param {Object} state 状态
       * @param {Object} dictionary 字典项
       * @returns {*} null
       */
      pushDictionary (state, dictionary) {
        if (!state) {
          state = {}
        }
        state.dictionarys.concat(dictionary)
      },
      /**
       * 覆盖所有字典
       * @param {Object} state 状态
       * @param {Array} dictionarys 字典列表
       * @returns {*} null
       */
      setDictionarys (state, dictionarys) {
        state.dictionarys = dictionarys
      },
      /**
       * 左边菜单栏
       * @param {Object} state 状态
       * @param {Arrary} navMenus 菜单数据
       * @returns {*}
       */
      setNavMenus (state, navMenus) {
        state.navMenus = navMenus
      }
    },
    actions: {
      /**
       * 获取所有字典项
       * @param {Object} commit 提交信息
       * @param {Object} state 状态
       * @param {String} type 类型，type === 'reload'时，重新加载数据字典
       * @returns {*|AxiosPromise} 响应
       * @constructor
       */
      GetDictionarys ({commit, state}, type) {
        if (!state.dictionarys || state.dictionarys.length === 0 || type === 'reload') {
          return new Promise((resolve, reject) => {
            sysDict.dictAll().then(response => {
              if (response.msg === '执行成功') {
                const data = response.data
                commit('setDictionarys', data)
                resolve(data)
              } else {
                reject(response)
              }
            }).catch(error => {
              reject(error)
            })
          })
        }
      },
      /**
     * 获取所有字典项
     * @param {Object} commit 提交信息
     * @param {Object} state 状态
     * @param {String} sys 子系统
     * @param {String} dictTypeId 类型id
     * @returns {*|AxiosPromise} 响应
     * @constructor
     */
      GetDictionary ({commit, state}, {sys, dictTypeCode}) {
        return new Promise((resolve, reject) => {
          if (state.dictionarys && state.dictionarys.length > 0) {
            let dicts = state.dictionarys.filter(function (dict) {
              if (sys && dict.sys !== sys) {
                return false
              }
              if (dictTypeCode && dict.dictTypeCode !== dictTypeCode) {
                return false
              }
              return true
            })
            if (dicts && dicts.length > 0) {
              resolve(dicts)
              return
            }
          }
          sysDict.dictAll({sys, dictTypeCode}).then(response => {
            if (response.success) {
              const data = response.data
              commit('pushDictionary', data)
              resolve(data)
            } else {
              reject(response)
            }
          }).catch(error => {
            reject(error)
          })
        })
      },
      /**
       * 获取菜单数据
       * @param {Object} commit 提交信息
       */
      GetNavMenus ({commit, state}, {userId, reload}) {
        if (state.navMenus.length === 0 || reload) {
          return new Promise((resolve, reject) => {
            sysMenu.getMenus({userId}).then(res => {
              if (res.msg === '执行成功') {
                setTimeout(() => {
                  commit('setNavMenus', buildMenus(res.data))
                  resolve()
                }, 100)
              } else {
                reject(res)
              }
            })
          })
        }
      }
    }
 }
 export default sys
